package com.fcl.utils
{
	import mx.messaging.management.Attribute;
	
	import org.as3commons.reflect.Accessor;
	import org.as3commons.reflect.Type;

	public class XMLTools
	{
		
		private static function makeXML(data:Object,rootNodeName:String):XML{
			var xmlData:XML = new XML();
			var dataNode:XML;
			xmlData = <{rootNodeName}></{rootNodeName}>;
			var type:Type = Type.forInstance(data);	
			for each (var prop:Accessor in type.accessors){
				if(prop.isReadable() && data.hasOwnProperty(prop.name)){
					dataNode = new XML();
					if(data[prop.name] is String || data[prop.name] is int || data[prop.name] is Number 
						|| data[prop.name] is Boolean){
						dataNode = <{prop.name}>{data[prop.name]}</{prop.name}>;
						xmlData = xmlData.appendChild(dataNode);	
					}else if(data[prop.name] is Array){
						xmlData = xmlData.appendChild(makeXMLCollection(data[prop.name],prop.name));
					}else if(data[prop.name] is Object){
						xmlData = xmlData.appendChild(makeXML(data[prop.name],prop.name));
					}else{
						
					}
				}
			}
			return xmlData;
		}
		
		/**
		 * Object To XML Conversion Mechanism For Table 
		 * @param data
		 * @param rootNodeName
		 * @return 
		 * 
		 */		
		private static function makeXMLCollection(data:Object,rootNodeName:String):XML{
			var xmlData:XML = new XML();
			var itemNode:XML;
			xmlData = <{rootNodeName}></{rootNodeName}>;
			for( var i:int;i<data.length;i++){
				xmlData = xmlData.appendChild(makeXMLCollectionItem(data[i]));
			}
			
			return xmlData;
		}
		
		private static function makeXMLCollectionItem(data:Object):XML{
			var xmlData:XML = new XML();
			var dataNode:XML;
			xmlData = <item></item>;
			var type:Type = Type.forInstance(data);	
			for each (var prop:Accessor in type.accessors){
				if(prop.isReadable() && data.hasOwnProperty(prop.name)){
					dataNode = new XML();
					if(data[prop.name] is String || data[prop.name] is int || data[prop.name] is Number 
						|| data[prop.name] is Boolean){
						dataNode = <{prop.name}>{data[prop.name]}</{prop.name}>;
						xmlData = xmlData.appendChild(dataNode);	
					}else if(data[prop.name] is Array){
						xmlData = xmlData.appendChild(makeXMLCollection(data[prop.name],prop.name));
					}else{
						xmlData = xmlData.appendChild(makeXML(data[prop.name],prop.name));
					}
				}
			}
			return xmlData;
		}
		
		public static function convertFlexObjectToXML(data:Object,rootNodeName:String,withNamespace:Boolean= true,type:String = 'Structure'):XML{
			var ns:Namespace = new Namespace('http://www.sap.com/abapxml');
			var finalXML:XML = new XML();
			var tempXML:XML = new XML();
			finalXML.addNamespace(ns);
			if(type == 'Table'){
				tempXML = makeXMLCollection(data,rootNodeName);
			}else{
				tempXML = makeXML(data,rootNodeName);
			}
			
			if(withNamespace){
				finalXML = <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"><asx:values>{tempXML}</asx:values></asx:abap>
			}else{
				finalXML = tempXML;
			}			
			return finalXML;
		}
	}
}